package com.gitee.wsl.compose.ui.dialog

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.gitee.wsl.compose.modifier.layout.outerPadding
import com.gitee.wsl.compose.ui.base.Button
import com.gitee.wsl.compose.ui.base.Dialog
import com.gitee.wsl.compose.ui.base.Outlined
import com.gitee.wsl.compose.ui.base.Text
import com.gitee.wsl.compose.ui.base.TextField
import com.gitee.wsl.compose.ui.text.Label
import com.gitee.wsl.compose.ui.theme.LocalDimens


/**
 * Yes Dialog.
 */
@Composable
fun Dialog.Yes(
    onDismissRequest: () -> Unit,
    properties: DialogProperties = DialogProperties(),
    title: String,
    content: String,
    confirmText: String
) {
    Dialog.Basic(
        onDismissRequest = onDismissRequest,
        properties = properties
    ) {
        DialogTitle(text = title)
        ItemOuterTip(text = content)
        Button.Text(
            onClick = {
                onDismissRequest()
            },
            modifier = Modifier
                .fillMaxWidth()
                .outerPadding(),
            text = confirmText
        )
    }
}

/**
 * Yes or No Dialog.
 */
@Composable
fun Dialog.YesNo(
    onDismissRequest: () -> Unit,
    onConfirm: () -> Unit,
    properties: DialogProperties = DialogProperties(),
    title: String,
    content: String,
    drawContent: (@Composable () -> Unit)? = null,
    cancelText: String ,
    confirmText: String
) {
    Dialog.Basic(
        onDismissRequest = onDismissRequest,
        properties = properties
    ) {
        DialogTitle(text = title)
        ItemOuterTip(text = content)
        drawContent?.invoke()
        Row(
            modifier = Modifier.outerPadding()
        ) {
            Button.Text(
                onClick = {
                    onDismissRequest()
                },
                modifier = Modifier.weight(1f),
                text = cancelText,
                //textColor = MaterialTheme.colors.subText,
                //backgroundColor = SaltTheme.colors.subBackground
            )
            Spacer(modifier = Modifier.width(LocalDimens.current.padding))
            Button.Text(
                onClick = {
                    onConfirm()
                },
                modifier = Modifier
                    .weight(1f),
                text = confirmText
            )
        }
    }
}

/**
 * Input Dialog.
 */
@Composable
fun Dialog.Input(
    onDismissRequest: () -> Unit,
    onConfirm: () -> Unit,
    properties: DialogProperties = DialogProperties(),
    title: String,
    text: String,
    onChange: (String) -> Unit,
    hint: String? = null,
    cancelText: String ,
    confirmText: String
) {
    Dialog.Basic(
        onDismissRequest = onDismissRequest,
        properties = properties
    ) {
        DialogTitle(text = title)

        val focusRequester = remember { FocusRequester() }
        TextField.Outlined(
            value = text,
            onValueChange = onChange,
            //hint = hint,
            modifier = Modifier.focusRequester(focusRequester)
        )
//        ItemOuterEdit(
//            text = text,
//            onChange = onChange,
//            hint = hint,
//            modifier = Modifier.focusRequester(focusRequester)
//        )
        LaunchedEffect(Unit) {
            focusRequester.requestFocus()
        }

        Row(
            modifier = Modifier.outerPadding()
        ) {
            Button.Text(
                onClick = {
                    onDismissRequest()
                },
                modifier = Modifier
                    .weight(1f),
                text = cancelText,
                //textColor = SaltTheme.colors.subText,
                //backgroundColor = SaltTheme.colors.subBackground
            )
            Spacer(modifier = Modifier.width(LocalDimens.current.padding))
            Button.Text(
                onClick = {
                    onConfirm()
                },
                modifier = Modifier
                    .weight(1f),
                text = confirmText
            )
        }
    }
}

/**
 * The basic dialog has default corner background and vertical padding.
 */
@Composable
fun Dialog.Basic(
    onDismissRequest: () -> Unit,
    properties: DialogProperties = DialogProperties(),
    content: @Composable () -> Unit
) {
    Dialog(
        onDismissRequest = onDismissRequest,
        properties = properties
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .clip(MaterialTheme.shapes.large)
                .background(color = MaterialTheme.colorScheme.background)
                .outerPadding(horizontal = false)
        ) {
            content()
        }
    }
}

@Composable
fun DialogTitle(
    text: String
) {
    Text(
        text = text,
        modifier = Modifier.outerPadding(),
        fontSize = 18.sp,
        fontWeight = FontWeight.Bold
    )
}

@Composable
fun ItemOuterTip(text: String) {
    Text(
        text = text,
        modifier = Modifier.outerPadding(),
        style = MaterialTheme.typography.titleSmall
    )
}